package com.wsz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.pojo.R;
import com.wsz.pojo.User;
import com.wsz.pojo.UserInfo;
import com.wsz.pojo.dto.PasswordDto;
import com.wsz.pojo.dto.RegisterUserDto;
import com.wsz.pojo.dto.UserDto;
import com.wsz.user.service.UserInfoService;
import com.wsz.user.service.UserService;
import com.wsz.user.mapper.UserMapper;
import com.wsz.utils.SecurityContextUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wangs
* @description 针对表【user】的数据库操作Service实现
* @createDate 2023-01-19 13:58:58
*/

@AllArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    private final UserInfoService userInfoService;

    @Override
    public R register(RegisterUserDto registerUserDto) {
        String username = registerUserDto.getUsername();
        String password = registerUserDto.getPassword();
        String checkPassword = registerUserDto.getCheckPassword();

        if(StringUtils.isBlank(username)){
            return R.error().message("用户名不能为空");
        }

        if(StringUtils.isBlank(password)){
            return R.error().message("密码不能为空");
        }

        if(StringUtils.isBlank(checkPassword)){
            return R.error().message("确认密码不能为空");
        }

        if(username.length() > 25){
            return R.error().message("用户名过长");
        }

        if(!password.equals(checkPassword)){
            return R.error().message("两次密码不一致");
        }

        password = new BCryptPasswordEncoder().encode(password);

        if(password.length() > 100) {
            return R.error().message("密码过长");
        }

        User user = this.getUserByUsername(username);

        if(user != null) {
            return R.error().message("该用户已存在");
        }

        User user1 = new User();
        user1.setUsername(username);
        user1.setPassword(password);
        user1.setEnabled(true);

        this.save(user1);

        User userByUsername = this.getUserByUsername(username);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(userByUsername.getId());
        userInfo.setUsername(username);
        userInfo.setPhoto("https://s.cn.bing.net/th?id=OHR.FriedensglockeFichtelberg_ZH-CN5510489151_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&qlt=50");

        userInfoService.save(userInfo);

        return R.ok();
    }

    public User getUserByUsername(String username) {
        return this.getOne(new QueryWrapper<User>().eq("username",username));
    }

    @Override
    public R updatePassword(PasswordDto passwordDto) {

        String username = SecurityContextUtils.getUserUserName();

        String password = passwordDto.getPassword();
        String checkPassword = passwordDto.getCheckPassword();

        if(StringUtils.isBlank(password)){
            return R.error().message("密码不能为空");
        }

        if(StringUtils.isBlank(checkPassword)){
            return R.error().message("确认密码不能为空");
        }

        if(!password.equals(checkPassword)){
            return R.error().message("两次密码不一致");
        }

        password = new BCryptPasswordEncoder().encode(password);

        this.baseMapper.updatePasswordByUsername(username,password);

        return R.ok();
    }

    @Override
    public R getUserList(Long page, Long limit) {
        Long total = this.baseMapper.selectCount(null);
        List<UserDto> userList = baseMapper.getUserList((page - 1) * limit,limit);

        return R.ok().data("total",total).data("userList",userList);
    }

    @Override
    public void updateEabled(String userId,Boolean enabled) {
        this.baseMapper.updateEnabledByUserId(userId,enabled);
    }

    @Override
    public Integer getRegisterCount(String date) {
        return this.baseMapper.getRegisterCount(date);
    }

}




